package cn.wp.org.controller;

import cn.wp.basic.util.AjaxResult;
import cn.wp.basic.util.PageList;
import cn.wp.org.domain.Shop;
import cn.wp.org.query.ShopQuery;
import cn.wp.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 入驻店铺
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }

    /**
     * 分页
     * @param shopQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个门店对象",notes = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult delect(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除",notes = "")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    /**
     * 通过审核
     * @param shop
     * @return
     */
    @ApiOperation(value = "通过审核",notes = "")
    @PutMapping
    public AjaxResult passShop(@RequestBody Shop shop){
        try {
            shopService.passShop(shop);
            return new AjaxResult().setMsg("通过成功，待用户审核");
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 单击邮箱激活店铺
     * @param id
     * @return
     */
    @GetMapping("/activation")
    public String activation(Long id){
        try {
            shopService.activation(id);
            return "<h1>激活成功</h1>";
        } catch (Exception e){
            e.printStackTrace();
            return "<h1>激活失败</h1>";
        }
    }

    /**
     * 驳回操作
     * @param shop
     * @return
     */
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody Shop shop){
        try {
            shopService.reject(shop);
            return new AjaxResult().setMsg("驳回成功！");
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("驳回失败！");
        }
    }
}
